package com.jiezhuo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.entity.RoleMenu;
import com.jiezhuo.service.RoleMenuService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2022-10-05
 */
@RestController
@RequestMapping("/role-menu")
public class RoleMenuController {

    @Autowired
    RoleMenuService roleMenuService;
    @ApiOperation("添加用户角色关联")
    @PostMapping("saveCategory")
    public R saveOrUpdateCategory(String roleId ,String[] menus){
        ArrayList<RoleMenu> roleMenus = new ArrayList<>();
        QueryWrapper<RoleMenu> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("role_id",roleId);
        roleMenuService.getBaseMapper().delete(objectQueryWrapper);
        for (int i = 0; i < menus.length; i++) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menus[i]);
            roleMenus.add(roleMenu);
        }
        boolean b = roleMenuService.saveBatch(roleMenus);
        if(b) {
            return R.ok().msg("添加成功");
        }
        return R.ok().msg("添加失败");
    }

    @ApiOperation("删除用户角色关联")
    @PostMapping("removeCategory")
    public R removeCategory(String roleId){
        QueryWrapper<RoleMenu> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("role_id",roleId);
        int delete = roleMenuService.getBaseMapper().delete(objectQueryWrapper);
        return R.ok().msg("删除成功");
    }
//    @ApiOperation("查询产品")
//    @PostMapping("selectCategory")
//    public R selectCategory(String[] ids){
//        QueryWrapper<Shop> objectQueryWrapper = new QueryWrapper<>();
//        if(ids.length>0){
//            objectQueryWrapper.in("id",ids);
//        }
//        List<Shop> shops = shopService.getBaseMapper().selectList(objectQueryWrapper);
//
//        if(shops.size()>0){
//            return R.ok().msg("查询成功").data("categories",shops).code(2000);
//        }
//        return R.ok().msg("查询失败").code(4000);
//    }

}

